<?php
	class general {
		/**
		* Parse SEO url to Request 
		*/
		static function parse_url_to_request(){
			// take the string after web domain
			$url = substr($_SERVER['REQUEST_URI'],strlen(constant('WORKING_DIR')));
			// checks for variables in url
			$var_start_pos = strpos($url, '?');
			if ($var_start_pos !== false){
				$url = substr($url, 0, $var_start_pos);
			}
			// add all variables to request
			$_REQUEST = array_merge($_REQUEST, $_GET, $_POST);
			
			// if there is no controller nor view request, load default controller and view
			if ($url == ''){
				general::redirect(constant('STARTING_PAGE'));
				return;
			}
			
			// if the template is prohibited, throw error ('or just don't add it to templates, it throw error automatically')
			if(in_array($url, $GLOBALS['PROHIBITED_TEMPLATES'])){
				return;
			}
			
			// if it's redirectable, replace it with redirection
			if(isset($GLOBALS['REDIRECT_TEMPLATES'][$url])){
				$url = $GLOBALS['REDIRECT_TEMPLATES'][$url];
			}
			
			$url_parsed = explode('/', $url);
			if(count($url_parsed) >= 1){
				// at last, get controller and view name and path from url
				$_REQUEST['template']=$url_parsed[0];
				$_REQUEST['controller']=(empty($url_parsed[1])?$url_parsed[0]:$url_parsed[1]).'.php';
				$_REQUEST['controller_path']=$_REQUEST['template'].'/'.$_REQUEST['controller'];
				$_REQUEST['view']=(empty($url_parsed[1])?$url_parsed[0]:$url_parsed[1]).'.tpl';
				$_REQUEST['view_path']=$_REQUEST['template'].'/'.$_REQUEST['view'];
				$_REQUEST['additional'] = array_slice($url_parsed, 2);
			}
		}
		/**
		* Remember debug messages
		* 
		*/
		static function debug($message, $variable){
			// if debug_mode is on
			if (isset($_SESSION['debug_mode']) && $_SESSION['debug_mode'] == 1){
				// generate some information about calling
				$backtrace = debug_backtrace();
				$backtrace = $backtrace[0];
				// generate filename
				$debug_info['file'] = array_reverse(explode('\\', $backtrace['file']));
				$debug_info['file'] = $debug_info['file'][0];
				// line
				$debug_info['line'] = $backtrace['line'];
				// memory and time usage
				$debug_info['time'] = 0;
				$debug_info['memory'] = round(memory_get_usage() / 1024);
				if(isset($GLOBALS['debug_stats'])){
					$debug_info['time'] = microtime(true) - $GLOBALS['debug_stats']['time'];
				}
				$GLOBALS['debug_stats']['time'] = microtime(true);
				
				$debug_info['message'] = $message;
				ob_start();
				var_dump($variable);
				$debug_info['variable'] = ob_get_clean();
				// finally mark down desired message and variable
				$GLOBALS['debug_messages'][] = $debug_info;
			}
		}
		/**
		* include all files in DirectoryIteratory
		*
		*/
		static function includeDir($dirname){
			if (is_dir($dirname)){
				$handle = opendir($dirname);
				while ($filename = readdir($handle)){
					if(substr($filename, 0, 1) != '.'){
						include_once($dirname.$filename);	
					}					
				}	
			}
		}
		
		/**
		* redirect to url
		*
		*/
		static function redirect($url){
			header('Location: '.$url);
		}
		
		/**
		 * Sends mail
		 * 
		 * @param recipients - array containing [to], [cc], [bcc] in format [name][email] (name can be NULL)
		 * cc bcc not implemented yet
		 * @param $message - array containig [subject], [message]
		 * @param $from - string containig email
		 * @param $footer - string containing footer, can be NULL
		 */
		static function send_mail($recipients, $message, $from = MAIL_DEFAULT){
			$to = "";	
			if (is_string($recipients)){
				$to = $recipients;	
			} else {
				$to = array();
				foreach ($recipients["to"] as $recipient) {
					if(!empty($recipient["name"])){
						$to[] = $recipient["name"]." <".$recipient["email"].">";
					}
				}
				$to = implode(', ', $to);
			}	
			$add_headers  = 'MIME-Version: 1.0' . "\r\n";
			$add_headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
			
			$add_headers .= 'From: ' . $from. "\r\n";
			mail($to, $message["subject"], $message["message"], $add_headers);
		}
		
		static function title($new_title){
			$GLOBALS["title"] = TITLE_PREFIX . $new_title;
		}
	}
?>